package com.mwj.generator.jpa;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.Data;
import lombok.experimental.Accessors;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.SelectBeforeUpdate;
import org.springframework.data.annotation.CreatedBy;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedBy;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;
import org.springframework.format.annotation.DateTimeFormat;

import javax.persistence.Column;
import javax.persistence.EntityListeners;
import javax.persistence.MappedSuperclass;
import java.io.Serializable;
import java.time.LocalDateTime;

/**
 * @author Meng Wei Jin
 * @description
 * @date Create in 2019-07-28 15:18
 **/
@Data
@Accessors(chain = true)
@DynamicInsert
@SelectBeforeUpdate
@DynamicUpdate
@MappedSuperclass
@EntityListeners(AuditingEntityListener.class)
public abstract class BaseEntity implements Serializable {

    private static final String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";

    @CreatedBy
    @JsonIgnore
    @Column(name = "CREATE_BY")
    protected String createBy;

    @CreatedDate
    @DateTimeFormat(pattern = YYYY_MM_DD_HH_MM_SS)
    @JsonFormat(pattern = YYYY_MM_DD_HH_MM_SS)
    @Column(name = "CREATE_TIME")
    protected LocalDateTime createTime;

    @LastModifiedBy
    @JsonIgnore
    @Column(name = "UPDATE_BY")
    protected String updateBy;

    @LastModifiedDate
    @DateTimeFormat(pattern = YYYY_MM_DD_HH_MM_SS)
    @JsonFormat(pattern = YYYY_MM_DD_HH_MM_SS)
    @Column(name = "UPDATE_TIME")
    protected LocalDateTime updateTime;

    /**
     * 删除标记 --系统只做逻辑删除
     */
    @Column(name = "DELETED")
    protected Boolean deleted = Boolean.FALSE;

    /**
     * 启用标记 --默认启用
     */
    @Column(name = "ENABLED")
    protected Boolean enabled = Boolean.TRUE;
}
